

#ifndef __CORTISOL_AVFORMAT_H
#define __CORTISOL_AVFORMAT_H

class AVFormatLib
{
	HMODULE handle;

	typedef void (*av_register_all_fptr_t)();
	typedef int  (*av_seek_frame_fptr_t)(AVFormatContext *s, int stream_index, int64_t timestamp,int flags);
	typedef int  (*av_find_stream_info_fptr_t)(AVFormatContext *ic);
	typedef int  (*av_open_input_file_fptr_t)(AVFormatContext **ic_ptr, const char *filename,AVInputFormat *fmt,int buf_size,AVFormatParameters *ap);
	typedef void (*av_close_input_file_fptr_t)(AVFormatContext *s);
	typedef int  (*av_read_frame_fptr_t)(AVFormatContext *s, AVPacket *pkt);

	av_register_all_fptr_t av_register_all_fptr;
	av_seek_frame_fptr_t av_seek_frame_fptr;
	av_find_stream_info_fptr_t av_find_stream_info_fptr;
	av_open_input_file_fptr_t av_open_input_file_fptr;
	av_close_input_file_fptr_t av_close_input_file_fptr;
	av_read_frame_fptr_t av_read_frame_fptr;

public:

	AVFormatLib() {

		Initialise();
	}

	virtual ~AVFormatLib() {

		CleanUp();
	}

	void av_register_all(void) const {

		av_register_all_fptr();
	}

	int  av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,int flags) const {

		return av_seek_frame_fptr(s, stream_index, timestamp, flags);
	}

	int  av_find_stream_info(AVFormatContext *ic) const {

		return av_find_stream_info_fptr( ic );
	}

	int  av_open_input_file(AVFormatContext **ic_ptr, const char *filename,AVInputFormat *fmt,int buf_size,AVFormatParameters *ap) const {

		return av_open_input_file_fptr(ic_ptr,filename,fmt,buf_size,ap);
	}

	void av_close_input_file(AVFormatContext *s) const {

		av_close_input_file_fptr(s);
	}

	int  av_read_frame(AVFormatContext *s, AVPacket *pkt) const {

		return av_read_frame_fptr( s, pkt );
	}

private:

	void Initialise() {

		if((handle = LoadLibrary( L"avformat-52.dll" )) == NULL)
			throw Exception("LoadLibrary( L\"avformat-52.dll\" )");

		RESOLVE(av_register_all);
		RESOLVE(av_seek_frame);
		RESOLVE(av_find_stream_info);
		RESOLVE(av_open_input_file);
		RESOLVE(av_close_input_file);
		RESOLVE(av_read_frame);
	}

	void CleanUp() {

		FreeLibrary( handle );
	}

};

#endif /*** __CORTISOL_AVFORMAT_H ***/

